<template>
    <div id="economy" class="economy">

    </div>
</template>

<script>
export default {
    mounted() {
        this.initChart()
    },
    methods: {
        initChart() {
            const chart = this.$echarts.init(document.getElementById('economy'));
            const options = {
                title: {
                    text: '经济增长情况',
                    textStyle: {
                        color: 'rgb(85, 85, 85)',
                        fontSize: 14,
                        fontWeight: 100
                    }

                },
                grid: {
                    top: '20%',
                    left: '5%',
                    right: '5%',
                    bottom: '8%',
                    containLabel: true,
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'line',
                        
                    },
                    formatter(params) {
                        var data = '';
                        for (var i = 0; i < params.length; i++) {
                            if (params[i].seriesName == 'GDP增长率(年百分比)') {
                                data += params[i].seriesName + ':  ' + params[i].value + '%';
                            } else {
                                data += params[i].seriesName + ':  ' + params[i].value + '<br/>';
                            }
                        }
                        return data;
                    },
                },
                legend: {
                    data: ['GDP增长率(年百分比)', 'GDP(人民币)'],
                    orient: 'vertical',
                    left: 'center',
                    bottom: 'bottom',
                    padding: [10, 0, 5, 0],
                    textStyle: {
                        color: '#747474',
                    },
                },
                xAxis: {
                    type: 'category',
                    data: this.economydata.datax,
                    axisLine: {
                        show: true,
                    },
                    axisTick: {
                        show: true,
                    },
                    axisLabel: {
                        show: true,


                        textStyle: {
                            color: '#393939', //X轴文字颜色
                        },
                    },
                },
                yAxis: [
                    {
                        type: 'value',
                        // name: 'GDP(人民币)',
                        nameTextStyle: {
                            color: '#393939',
                        },
                        splitLine: {
                            show: false,
                            lineStyle: {
                                color: '#eeeeee',
                            },
                        },
                        axisTick: {
                            show: true,
                        },
                        axisLine: {
                            show: true,
                        },
                        axisLabel: {
                            show: true,
                            formatter: '{value} 万亿',
                            textStyle: {
                                color: '#393939',
                            },
                        },
                    },
                    {
                        type: 'value',
                        // name: '(%)',
                        nameTextStyle: {
                            color: '#393939',
                            padding: [0, 0, 0, 40], // 四个数字分别为上右下左与原位置距离
                        },
                        position: 'right',
                        splitLine: {
                            show: false,
                        },
                        axisTick: {
                            show: true,
                        },
                        axisLine: {
                            show: true,
                        },
                        axisLabel: {
                            show: true,
                            textStyle: {
                                color: '#393939',
                            },
                        },
                    },
                    {
                        type: 'value',
                        gridIndex: 0,
                        min: 50,
                        max: 100,
                        splitNumber: 8,
                        splitLine: {
                            show: false,
                        },
                        axisLine: {
                            show: false,
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLabel: {
                            show: false,
                        },
                        splitArea: {
                            show: false,
                            areaStyle: {
                                color: ['rgba(250,250,250,0.0)', 'rgba(250,250,250,0.05)'],
                            },
                        },
                    },
                ],
                series: [
                    {
                        name: 'GDP(人民币)',
                        type: 'bar',
                        barWidth: 15,
                        itemStyle: {
                            normal: {
                                color: '#a94442',
                                barBorderRadius: 12,
                            },
                        },
                        data: this.economydata.datay
                    },
                    {
                        name: 'GDP增长率(年百分比)',
                        type: 'line',
                        yAxisIndex: 1, //使用的 y 轴的 index，在单个图表实例中存在多个 y轴的时候有用
                        showAllSymbol: true, //显示所有图形。
                        symbolSize: 4, //标记的大小
                        itemStyle: {
                            //折线拐点标志的样式
                            color: '#444',
                            borderWidth: '2',
                            borderColor: '#444',
                            normal: {
                                label: {
                                    show: true, //开启显示数值
                                    position: 'top', //数值在上方显示
                                    textStyle: {  //数值样式
                                        color: '#444',   //字体颜色
                                        fontSize: 10,//字体大小

                                    },
                                    formatter(params) {
                                        let htmlStr = Math.abs(params.value) + '%'
                                        return htmlStr
                                    }
                                }
                            }

                        },
                        lineStyle: {
                            color: '#444',
                        },
                        data: this.economydata.datay1

                    },
                ],
            };
            chart.setOption(options);
            window.onresize = function () {
                chart.resize()
            }
        }
    },
    props: ['economydata']
}
</script>

<style lang="less" scoped>
.economy {
    padding-top: 5px;
    padding-left: 5px;
    width: 1232px;
    height: 275px;
    background-color: #fff;
    margin: 0 auto;
    margin-top: 15px;
}
</style>